window.onload = function ()
{
	console.log(`iframe.js连接成功`);
	let right_display = document.getElementById('right_display');
	let option = document.getElementsByClassName('option');
	option[4].addEventListener('click',()=>{
		//这里是订单查询的ajax 这里改为成绩查询
		
		
		/*下面改为了成绩查询的js*/
		
		// let data0 = new FormData()
		// data0.append("name",student);
		// data0.append("password",studentPWD);
		// const option0 = {
		// 	method : "POST",
		// 	body:data0
		// }
		// fetch("./PHP/selectScore.php",option0)
		// .then((response)=>{
		// 	if(response.ok)
		// 	{	
		// 		console.log("你在成绩查询处启动了ajax");
		// 		return response.text();
				
		// 	}
		// })
		// .then((data)=>{
		// 	right_display.innerHTML = data;
		// 	scoreColor();
		// })
		let xhr = new XMLHttpRequest();
		xhr.open("GET","./PHP/quwen.php",true);
		xhr.send(null);
		xhr.onreadystatechange = function (){
			if(xhr.readyState == 4){
				if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
					right_display.innerHTML = xhr.responseText;
					quwen();
				}
			}
		}
		
		
		
	});
	
	
	option[3].addEventListener('click',()=>{
		//查看留言墙的ajax
		let data = new FormData();
		data.append("name",student);
		data.append("password",studentPWD);
		
		let option3 = {
			method : "POST",
			body : data
		}
		
		fetch('./PHP/follow_message.php',option3)
		.then((response)=>{
			if(response.status == 200)
			{
				return response.text();
			}
			else
			{
				console.log("留言墙查看出错");
			}
		})
		.then((data)=>{
			right_display.innerHTML = data;
		})
		.catch(e=>{console.log(e.name+e.message)});
		setTimeout(follow_message,3000);
		function follow_message()
		{
			let center_follow = document.getElementsByClassName("center_follow");
			for(let i = 0 ; i < center_follow.length; i ++)
			{
				let random_backColor = Math.floor(Math.random()*10);
				switch(random_backColor)
				{
					case 1:
					center_follow[i].style.backgroundColor = "#707070";
					break;
					
					case 2:
					center_follow[i].style.backgroundColor = "#558188";
					break;
					
					case 3:
					center_follow[i].style.backgroundColor = "#5FA292";
					break;
					
					case 4:
					center_follow[i].style.backgroundColor = "#FF9C5F";
					break;
					
					case 5:
					center_follow[i].style.backgroundColor = "#FF8B6C";
					break;
					
					case 6:
					center_follow[i].style.backgroundColor = "#F4F4F2";
					break;
					
					case 7:
					center_follow[i].style.backgroundColor = "#B5B5B5";
					break;
					
					case 8:
					center_follow[i].style.backgroundColor = "#92DCE0";
					break;
					
					case 9:
					center_follow[i].style.backgroundColor = "#0092D7";
					break;
					
					default :
					center_follow[i].style.backgroundColor = "#EE8D09";
					break;
				}
				
			}
		}
	});
	
	option[2].addEventListener('click',()=>{
		// 学校留言墙的ajax 
		let data = new FormData();
		data.append("name",student);
		data.append("password",studentPWD);
		let option2 = {
			method : "POST",
			body : data
		}
		fetch("./message_school.php",option2)
		.then((response)=>{
			console.log(response.statusText);
			if(response.ok)
			{
				return response.text();
			}
			else
			{
				console.log("访问失败")
			}
		})
		.then((data)=>{
			right_display.innerHTML = data;
			message();
		})
		.catch((error)=>{
			console.log("你可能出错了"+error);
		})
	});
	
	option[1].addEventListener('click',()=>{
		// 明日早餐的ajax  这里改为查看订单
		
		
		/*下面已改为查看订单的ajax*/
		let xhr = new XMLHttpRequest();
		xhr.open("POST","./PHP/orderSelect.php",true);
		xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xhr.send('name='+student+'&password='+studentPWD);
		xhr.onreadystatechange = function ()
		{
			if(xhr.readyState == 4)
			{
				if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304)
				{
					right_display.innerHTML = xhr.responseText;
					// 这里可能需要调用函数
					setTimeout(lookup,3000);
					
				}
				else
				{
					console.log("订单查询处的ajax调用失败");
				}
			}
			
			
			
			
		}
		
	});
	
	option[0].addEventListener("click",()=>{
		// 成绩查询的ajax  这里改为明日早餐
		// let data0 = new FormData()
		// data0.append("name",student);
		// data0.append("password",studentPWD);
		// const option0 = {
		// 	method : "POST",
		// 	body:data0
		// }
		// fetch("./PHP/selectScore.php",option0)
		// .then((response)=>{
		// 	if(response.ok)
		// 	{	
		// 		console.log("你在成绩查询处启动了ajax");
		// 		return response.text();
				
		// 	}
		// })
		// .then((data)=>{
		// 	right_display.innerHTML = data;
		// 	scoreColor();
		// })
		
		/*下面改为明日早餐的ajax*/
		
		let xhr = new XMLHttpRequest();
		xhr.open("POST",'http://127.0.0.1/%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1/PHP/testfast.php',true);
		// 如果是post请求则需要设置请求头 xhr.setH...
		xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xhr.send('name='+student);
		xhr.timeout = 10000;
		xhr.onreadystatechange = function ()
		{
			if(xhr.readyState == 4)
		     {
			  if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304)
				{
					right_display.innerHTML = xhr.responseText;
					breakfast();
				}
				else
				{
						console.log('ajax请求失败');
				}
				}
			
		}
	});
	
	
	/*以下为修改密码弹窗的js*/
	let display_block = document.getElementById("display_block");
	let right_close = document.getElementById("right_close");
	let flex_1 = document.getElementById("flex_1");
	
	
	
	display_block.addEventListener("click",()=>{
		
		// flex_1.style.display = "block";
		flex_1.style.right = "0px";
	});
	
	right_close.addEventListener("click",()=>{
		
		// flex_1.style.display = "none";
		flex_1.style.right = "-300px";
	});
	
	// 明天任务，把密码的正则写好，
	// 并且获取两次输入的密码值的对比结果。（两次输入的密码不匹配！
	// 如果为错误就不可以提交 12.22
	
	/*密码弹窗的js到此结束*/
	
	/*以下是学号正则的JavaScript*/
	let change_name = document.getElementsByName("change_name")[0];//元素定位到输入学号文本框
	let stuNumber_tip = document.getElementsByClassName("stuNumber_tip")[0]; //元素定位到学号输入信息提示。
	change_name.addEventListener("blur",()=>{
		let change_name_value = change_name.value;
		if(!RegglarExpression_userID.test(change_name_value) || change_name_value != student)
		{
			stuNumber_tip.innerText = "与登录学号不一致";
			change_name.style.border = "2px solid red";
		}
		
		
	});
	
	change_name.addEventListener("focusin",()=>{
		
		stuNumber_tip.innerText = "";
		change_name.style.border = "2px solid #ffff";
	});
	
	
	
	/*学号正则的JavaScript结束*/
	
	
	/*以下是密码提交比较的js*/
	const RegglarExpression_password = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;//判断密码的正则表达式
	const RegglarExpression_userID = /^\d{8,}$/;//学号正则
	let newPWD = document.getElementsByName("newPWD")[0];
	let RX_change = document.getElementsByClassName("RX_change")[0];
	
	/*声明两个值的变量*/
	let  newPWD_value,once_value; 
	/***/
	newPWD.addEventListener('blur',()=>{
		  newPWD_value = newPWD.value;
		if(!RegglarExpression_password.test(newPWD_value))
		{
			RX_change.innerText = "密码必须以字母开头且包含数字,6-16位";
			// RX_change.style.color = "#a6404e";
			newPWD.style.border = "2px solid red";
		}
		
	});
	
	newPWD.addEventListener("focusin",()=>{
		
		RX_change.innerText = "";
		newPWD.style.border = "2px solid #ffff";
	});
	
	let once_again = document.getElementsByName("once_again")[0]; //输入新密码的元素定位
	let value_tip = document.getElementsByClassName("value_tip")[0];//错误新密码的提示的元素定位
	once_again.addEventListener("blur",()=>{
		 once_value = once_again.value;
		 if(newPWD_value != once_value)
		 {
			 value_tip.innerText = "两次输入的密码不一致";
			 once_again.style.border = "2px solid red";
		 }
	});
	
	once_again.addEventListener("focusin",()=>{
		
		value_tip.innerText = "";
		once_again.style.border = "2px solid #ffff";
	});
	
	
	let changePWD = document.getElementById("changePWD");//确认按钮的元素定位
	let form_up1 = document.getElementById("form_up1"); //表单提交的元素定位
	
	changePWD.addEventListener("click",()=>{
		
		change_name.focus();
		change_name.blur();
		
		newPWD.focus();
		newPWD.blur();
		
		once_again.focus();
		once_again.blur();
		
		if(value_tip.innerText == "" && RX_change.innerText == "" && stuNumber_tip.innerText == "" )
		{
			form_up1.submit();
			// console.log(submit_up.submit());
		}
		else
		{
			alert("不可提交，密码输入有误，请检查。");
		}
		
	})
	
	/*密码提交比较的js到此结束*/
	/*明日早餐的JavaScript*/
	function breakfast()
	{
		let add = document.getElementById("add"); //元素定位
		const button_add = document.getElementById("button_add"); //元素定位到按钮
		let produck = document.getElementsByClassName("produck");  // 获取class名为produck的元素
		let form_up = document.getElementById("form_up");  //获取表单 
		let shopin = document.getElementById("shopin");//购物的inner HTML
		let count = 0; //计算总价格
		let summary = [];//创建一个数组来接收所有的商品名称。
		/*数组去重*/
		function uniq(array)
		{
			let temp = []; //首先创建一个新数组
			for(let i = 0 ; i < array.length; i++)
			{
				if(temp.indexOf(array[i]) == -1) //遍历数组中的值,如果那个值不存在的话会返回-1,如果那个值已经有索引下标的话，我们则不需要把它再添加进来;
				{
					temp.push(array[i]); //新数组不存遍历中的那个值 我们就把它 push进去
				}
			}
			return temp;
		}
		/*数组去重end*/
		
		
		/*自己写的函数* 首先找到与值相对应的索引，然后再根据索引去操作删除数组 */
		function delete1(name) //传入你想要查找的数值 还可以在函数多写一个传入数组名字的入口
		{
		 for(let i = 0 ; i < summary.length;i ++)  //遍历数组寻找数值 
		 {
		  if(summary[i] == name) //寻找数值，如果数组匹配
		  {
		     return summary.splice(i,1); //则返回当前的索引并且对数组的下标进行操作，把数组当前下标对应的值删除。
		  }
		 }			
		}
		
		/*自己写的函数end*/
		
		//let StrProduct = ""; //创建一个空字符串 / 全局字符串有字符遗留问题被暂时取消 全局字符串被正式取消,此条注释留作纪念。
		for(let i = 0 ; i < produck.length; i++) //遍历produck
		{
			produck[i].onclick = function ()  //为每个produck加上鼠标单击事件
			{
				let click_string = '';
				let array_uniq;
				if(produck[i].checked) //判断鼠标是否勾选了当前的元素
				{
					count+=Number(produck[i].value); //获取当前勾选元素的值 并与初始化的值相加
					add.innerText = count; //在价格显示出显示价格
					console.log(`你选选择的商品为：${produck[i].name}`);
					summary.push(produck[i].name);
					array_uniq = uniq(summary);
					shopin.innerHTML = array_uniq.join("-").toString();
					
				}
				else
				{
					count-=Number(produck[i].value); //如果取消勾选则减去当前的价格
					add.innerText = count; //在显示价格处显示当前价格
					// summary.remove(produck[i].name);
					delete1(produck[i].name);
					console.log(`你已经取消的商品：${produck[i].name}`);
					array_uniq = uniq(summary);
					// for(let i = 0; i < array_uniq.length; i++)
					// {
					// 	click_string += array_uniq[i]+",";
					// }
					shopin.innerHTML = array_uniq.join("-").toString();
					
				}
			}
		}
		
		
		button_add.onclick = function ()  //为提交价格按钮添加鼠标单击事件
		{
			if(count != 0)  //判断当前的提交总价格是否不等于0
			{
				let StrProduct = "";
				for(let i = 0 ; i < summary.length; i++)
				{
					 StrProduct += summary[i] + "-";
				}
				
				if(confirm(`选中商品的总价格为${count}元,商品有${StrProduct}确定提交吗？`)) //添加一个确认按钮 如果确认则返回true
				{	
					
							const div_wrapper = document.createElement("div"); //创建一个最大的div
					        document.body.appendChild(div_wrapper); //将最大的div放映到body中
					        div_wrapper.setAttribute("id","wrapper_pay"); // 设置最大的div的属性
					        const div_close = document.createElement("div"); // 创建一个关闭按钮的div
					        div_wrapper.appendChild(div_close);//把关闭按钮的div放映到最大div里面
					        div_close.setAttribute("id","close_pay");//设置关闭按钮的div的属性
					        div_close.appendChild(document.createTextNode("x")); //在关闭div按钮中放映一文本X
					        div_close.onclick = function ()//给关闭的div加一个点击事件
					        {
								clearTimeout(cleart);
					            div_wrapper.remove();
					            alert("交易失败");
					        }
					        const div_center = document.createElement("div"); //创建一个内容的div
					        div_center.setAttribute("id","center_pay");//给内容的div加一个id属性
					        div_wrapper.appendChild(div_center);//把内容的div放映到最大的div里面
					        const text = document.createTextNode("支付界面\n假装这里有个支付码");//创建一个文本
					        const imga = document.createElement("img"); //创建一个img元素
					        imga.setAttribute("src","./images/pay.png");//设置img元素的属性
					        div_center.appendChild(text);//把文本放映到内容的div里面
					        div_center.appendChild(imga); //把图片放映到内容div里面
					       let cleart = setTimeout(() => { //放置一个定时器 四秒后开启
					            // div_wrapper.remove();
					            div_center.style.color = "red";
					            div_center.innerText = "支付成功";
					            /*这里放置ajax*/
								let xhr = new XMLHttpRequest();
								xhr.open("POST","./PHP/Order_processing.php",true);
								xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
								xhr.send("name="+student+"&pwd="+studentPWD+"&processing="+summary.join("-").toString()+"&money="+count);
								xhr.addEventListener('readystatechange',()=>{
									if(xhr.readyState == 4)
									{
										if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304)
										{
											alert(xhr.responseText);
										}
									}
									
									
								})
					
					
					            /*提交订单的ajax结束*/
					        setInterval(()=>{ //放置一个定时器，每隔6秒执行一次
					            if(div_center.innerText == "支付成功")
					        {
					            div_wrapper.remove();
					        }
					        },3000);
					        }, 4000);
				}
				else
				{
				
					alert("你已取消订单");
				    console.log(StrProduct);
					console.log(summary);
				}
			}
			else //如果选择的商品价格为0则不能提交
			{
				alert("你貌似没有选择商品");
			}
		}
		
		/*清空和全选按钮的js*/
		let empty = document.getElementById("empty");
		let allCheckbox = document.getElementById("allCheckbox");
		let clickNumber = 0;
		empty.addEventListener("click",()=>{
			for(let i = 0; i < produck.length; i++)
			{
				produck[i].checked = false;
			}
			summary = [];
			count = 0;
			add.innerText = count;
			shopin.innerHTML = "";
			clickNumber = 0;
		});
		
		allCheckbox.addEventListener('click',()=>{
			
			if(clickNumber < 1)
			{
				for(let i = 0; i < produck.length; i++)
				{
					produck[i].checked = true;
					summary.push(produck[i].name);
					count += Number(produck[i].value);
				}
				
				add.innerText = count;
				shopin.innerHTML = summary.join("-").toString();
			}
			clickNumber ++;
			
		});
		
		
	}
	/*明日早餐的结束*/
	
	
	/*下面是学校留言墙的js*/
	function message()
	{
		let submit_center = document.getElementById('submit_center'); //元素定位到提交按钮
		let c_p = document.getElementById('c_p'); //元素定位到span上的
		let form_message = document.getElementById('form_message');//
		let textareaWaH = document.getElementById('textareaWaH');//
		let textarea = document.getElementById('textareaWaH');//
		let submit_1 = document.getElementById('submit_1');
		let winW_H = setInterval(()=>{  //设置定时器每隔1秒钟运行一次
		let windowWidth = window.innerWidth,
		    windowHeight = window.innerHeight;
		if(typeof(windowWidth) == "number" && typeof(windowHeight) == "number")
		{
			
			if(windowWidth < 800 || windowHeight < 400) //判断浏览器的宽高
			{
				
				c_p.innerHTML = "<br />";
			}
			else
			{
				
				c_p.innerHTML = "";
			}
		}
		else //如果获取不到浏览器的大小
		{
			console.log(`获取不到浏览器窗口大小`);
			clearInterval(winW_H);
		}
		
		},1000);
		
		
		
		submit_1.addEventListener('click',submit_up);
		
		//提交判断函数
		function submit_up()
		{
			let value1 = textarea.value;
			  console.log("值为:"+value1);
			  console.log("长度为:"+value1.length);
			  if(value1 == "")
			  {
			  	console.log("内容为:"+textarea.value)
			  	alert("当前内容为空，不允许提交");
			  }
			  else
			 {
				if(confirm(`确定提交吗?`))
				{
					let xhr = new XMLHttpRequest();  //建立一个异步对象
					xhr.open("POST","./PHP/insmessage.php",true); // 设置传输方式 post 或 get 把第三个参数设置为true或false true为异步 false为同步 不过我们使用ajax就是为了异步而来的 所以要设为true
					xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // post要设置请求头
					xhr.send("newMessage="+value1+"&name="+student+"&password="+studentPWD); // post在这传参
					xhr.onreadystatechange = function ()  //当发生改变时 执行这个
					{
								 if(xhr.readyState == 4)  //是否已经完成了请求
								 {
									 if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) //判断状态码
									 {
										 alert(xhr.responseText);
									 }
									 else
									 {
										 alert("提交失败");
									 }
								 }
					}
					
				}
				else
				{
					alert('你已取消提交');
				}
			
			}
		}
		
		
		textareaWaH.addEventListener("textInput",(event)=>{
			// alert(`你输入了文本：${event.data}`);
			let random_1 = Math.floor( Math.random()*10 );
			console.log(`当前的随机数为${random_1}`);
			switch(random_1)
			{
				case 1:
				submit_1.style.backgroundColor = "#F78888";
				break;
				case 2:
				submit_1.style.backgroundColor = "#F3D250";
				break;
				case 3:
				submit_1.style.backgroundColor = "#FFCCCC";
				break;
				case 4:
				submit_1.style.backgroundColor = "#CCFF99";
				break;
				case 5:
				submit_1.style.backgroundColor = "#CCCCFF";
				break;
				case 6:
				submit_1.style.backgroundColor = "#77A6F7";
				break;
				case 7:
				submit_1.style.backgroundColor = "#FFCCBC";
				break;
				case 8:
				submit_1.style.backgroundColor = "#FFCB9A";
				break;
				case 9:
				submit_1.style.backgroundColor = "#D1E8E2";
				break;
				default:
				submit_1.style.backgroundColor = "#bb6e78"
				break;
			}
			
		})
		
	}
	/*学校留言墙的js结束*/
	
	/*早餐订单查询的js*/
	function lookup()
	{
		let createSelect = document.getElementsByClassName('createSelect');
		for(let i = 0 ; i < createSelect.length; i++)
		{
			let random_lookup = Math.floor(Math.random()*10);
			switch(true)
			{
				case random_lookup < 4:
				createSelect[i].style.backgroundColor = "#EBE3E0";
				break;
				case random_lookup <= 6:
				createSelect[i].style.backgroundColor = "EDD5D1";
				break;
				case random_lookup >= 7:
				createSelect[i].style.backgroundColor = "E3D6C6";
				break;
			}
			 
		}
		
	}
	/*早餐订单查询的js结束*/
	
	function scoreColor()
	{
		let mini_title = document.getElementsByClassName('mini_title');
		for(let i = 0 ; i < mini_title.length; i ++)
		{
			if(i%2 == 0)
			{
				mini_title[i].style.backgroundColor = "#F04C40";
			}
			else
			{
				mini_title[i].style.backgroundColor = "#FF8B6C";
			}
		}
		
		
	}
	
	
	// 校园周刊的js
	function quwen(){
		const quwen_title = document.getElementsByClassName("quwen_title");
		for(let i = 0 ; i < quwen_title.length; i++){
			quwen_title[i].addEventListener("click",()=>{
				let xhr = new XMLHttpRequest();
				xhr.open("GET","http://127.0.0.1/%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1/PHP/SX_Text.php?number="+quwen_title[i].getAttribute("data-log"),true);
				xhr.send();
				xhr.onreadystatechange = function (){
					if(xhr.readyState == 4){
						if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
							let quwenDiv_wrapper = document.createElement("div");
							let quwenDiv_close = document.createElement("div");
							let quwenText_close = document.createTextNode("×");
							quwenDiv_close.appendChild(quwenText_close);
							quwenDiv_close.setAttribute("class","quwen_close");
							quwenDiv_wrapper.appendChild(quwenDiv_close);
							quwenDiv_wrapper.setAttribute("id","div_wrapper");
							let quwen_center = document.createElement("div");
							quwen_center.style.clear = "both";
							quwen_center.innerHTML = xhr.responseText;
							quwenDiv_wrapper.appendChild(quwen_center);
							document.body.appendChild(quwenDiv_wrapper);
							quwenDiv_close.onclick = function (){
								quwenDiv_wrapper.remove();
							}
						}
					}
				}
				
			})
		}
		
	}
	
}